﻿using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web;

// ReSharper disable once CheckNamespace

namespace Web.Account
{
    public static class Email
    {
        private static readonly String EmailFrom = Constant.Parameter.Email.EmailFrom;
        private static readonly String SmtpServer = Constant.Parameter.Email.SmtpServer;
        private static readonly String Password = Constant.Parameter.Email.EmailPassword;

        /// <summary>
        ///     发送验证邮件
        /// </summary>
        /// <param name="emailTo">Email</param>
        /// <param name="userName">用户姓名</param>
        /// <param name="number">序列号</param>
        /// <param name="captcha">验证码</param>
        /// <returns>结果</returns>
        public static Boolean SendEmail(String emailTo, String userName, Int32 number, String captcha)
        {
            var message = new MailMessage {From = new MailAddress(EmailFrom, "Echizen Ryoma")};
            message.To.Add(new MailAddress(emailTo, userName));
            message.Subject = "注册验证邮件";

            var mailTemplatePath = HttpContext.Current.Server.MapPath("/Resource/MailCaptcha.html");
            var mailTemplate = new StreamReader(mailTemplatePath);
            var sendMailString = mailTemplate.ReadToEnd();
            sendMailString = sendMailString.Replace("<% UserName %>", userName);
            sendMailString = sendMailString.Replace("<% SendEmail %>", emailTo);
            sendMailString = sendMailString.Replace("<% Number %>", number.ToString("D2"));
            sendMailString = sendMailString.Replace("<% Captcha %>", captcha);
            sendMailString = sendMailString.Replace("<% DateTime %>", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sendMailString = sendMailString.Replace("<% Year %>",
                DateTime.Now.Year.ToString(CultureInfo.InvariantCulture));

            message.Body = sendMailString;
            message.IsBodyHtml = true;

            var emailSmtp = new SmtpClient(SmtpServer)
            {
                Credentials = new NetworkCredential(EmailFrom, Password)
            };
            try
            {
                emailSmtp.Send(message);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        ///     发送密码重置的验证邮件
        /// </summary>
        /// <param name="email">Email</param>
        /// <param name="userName">用户姓名</param>
        /// <param name="userId">用户ID</param>
        /// <param name="number">序列号</param>
        /// <param name="captcha">验证码</param>
        /// <returns>结果</returns>
        public static Boolean SendResetPasswordEmail(String email, String userName, String userId, Int32 number,
            String captcha)
        {
            var message = new MailMessage {From = new MailAddress(EmailFrom, "Echizen Ryoma")};
            message.To.Add(new MailAddress(email, "张磊"));
            message.Subject = "注册验证邮件";

            var mailTemplatePath = HttpContext.Current.Server.MapPath("/Resource/ResetPassword.html");
            var mailTemplate = new StreamReader(mailTemplatePath);
            var sendMailString = mailTemplate.ReadToEnd();
            sendMailString = sendMailString.Replace("<% UserName %>", userName);
            sendMailString = sendMailString.Replace("<% UserID %>", userId);
            sendMailString = sendMailString.Replace("<% Number %>", number.ToString("D2"));
            sendMailString = sendMailString.Replace("<% Captcha %>", captcha);
            sendMailString = sendMailString.Replace("<% DateTime %>", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            sendMailString = sendMailString.Replace("<% Year %>",
                DateTime.Now.Year.ToString(CultureInfo.InvariantCulture));

            message.Body = sendMailString;
            message.IsBodyHtml = true;

            var emailSmtp = new SmtpClient(SmtpServer) {Credentials = new NetworkCredential(EmailFrom, Password)};
            try
            {
                emailSmtp.Send(message);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}